package middleware

import (
	"lsgfish/server/internal/constant"
	"lsgfish/server/internal/tool"

	"github.com/gin-gonic/gin"
)

// 校验token
func ValidateToken(c *gin.Context) {
	// 从请求头获取token
	token := c.GetHeader("Authorization")
	if token == "" {
		tool.ErrF(c, constant.NOT_FOUND, "token不存在")
		c.Abort()
		return
	}

	// 验证token
	redisService := tool.NewRedisService()

	// 获取请求的ip
	// ip := c.ClientIP()
	if _, err := redisService.GetToken(c); err != nil {
		tool.Err(c, "token无效")
		c.Abort()
		return
	}

	c.Next()
}
